﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Messaging;
using MessageCommon.GlobalStructure;
using MessageCommon.GlobalType;
using MessageCommon;

namespace ReceiveClient
{
    public partial class Client : Form
    {
        ClientManager m_objManager;
        public Client()
        {
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            m_objManager = new ClientManager();
            NotificationCenter.PubicEventCenter.OneMsgReceiveFinished += new OneMsgReceiveFinishedEvendHandler(PubicEventCenter_OneMsgReceiveFinished);
        }

        void PubicEventCenter_OneMsgReceiveFinished(PushMessage msg)
        {
            ListViewItem item = new ListViewItem();
            item.Text = msg.strMagDepict;
            this.listView1.Items.Add(item);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            m_objManager.ReceiveOnLineMSG();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            m_objManager.StopReceiveMsg();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            

            ClientBase.Instance.UsersID = TextBoxUserName.Text;
            AddressInfo info = new AddressInfo();
            info.enumClientType = EnumClientType.PCClient;
            info.strIPAddress = GlobalFunc.GetLocalIP();
            info.strUserId = TextBoxUserName.Text;

            m_objManager.RegisterUser(info);
            button4.Enabled = false;
        }

        private void ButtonSend_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void TextBoxTargetName_TextChanged(object sender, EventArgs e)
        {
            ButtonSend.Enabled = true;
        }

        private void TextBoxMSG_TextChanged(object sender, EventArgs e)
        {
            ButtonSend.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            PushMessageList msgList = new PushMessageList();
            PushMessage msg = new PushMessage();
            msg.strTarget.Add(TextBoxTargetName.Text);
            msg.strMagDepict = TextBoxMSG.Text;
            msgList.Add(msg);
            m_objManager.SendOnlineMessage(msgList);
            ButtonSend.Enabled = false;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            timer1.Interval = int.Parse(textBox1.Text);
        }
    }
}
